// EspnTennisProject.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <opencv\cv.h>
#include <opencv2\nonfree\features2d.hpp>
#include <opencv\highgui.h>
#include "Frame.h"
#include "Pipeline.h"
#include "CourtModel.h"
#include "Line.h"
#include "ModelMatch.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

#include <boost\thread.hpp>

using namespace std;
using namespace cv;
// Main class. Entry point.

// Prints the correct usage of command parameters
void printUsage(){
	cout << "Usage:"<<endl;
	cout << "-f <video file name> \t input file"<<endl;
	cout << "-o <output file name> \t output file"<<endl;
	cout << "-m <model file name> \t court model file"<<endl;
	cout << "-p <true or t> \t \t true or t to track players"<<endl;
	cout << "example: EspnTennisProject.exe -f ../../tennis.mp4 -o output.mov -m ../../model.txt -p t\n \t EspnTennisProject.exe -f ../../tennis.mp4 -o output.mov -m ../../model.txt"<<endl;
	cin.get();
}


int main(int argc, char* argv[])
{		
	char* videoFile;
	char* modelFile;
	char* outFile;
	char* trackPlayer;

	videoFile = modelFile = outFile = trackPlayer = NULL;

	// if number parameters is less than expected print correct usage and example then exit.
	if (argc < 6) {
		printUsage();
		exit(0);
	} 

	//parse the command parameters.
	else {
		cout<<"Getting Params"<<endl;
		std::cout << argv[0]<<endl;
		for (int i = 1; i < argc; i++) {
			if (i + 1 != argc){
				//file name
				if (strcmp(argv[i], "-f") ==0) {
					videoFile = argv[i + 1];
					cout << videoFile << endl;
				} 
				//output name
				else if (strcmp(argv[i], "-o")==0) {
					outFile = argv[i + 1];
					cout << outFile <<endl;
				} 
				//model file name
				else if (strcmp(argv[i], "-m")==0) {
					modelFile = argv[i + 1];
					cout << modelFile <<endl;
				} 
				//track player or not
				else if (strcmp(argv[i], "-p")==0) {
					trackPlayer = argv[i + 1];
					cout << trackPlayer <<endl;
				} 
			}
		}
	}

	//if parameters not populated then print usage and exit.
	if(videoFile == NULL || modelFile == NULL || outFile == NULL){
		printUsage();
		exit(0);
	}

	CourtModel model(modelFile);
	//CourtModel model("../model_new.txt");
	//Pipeline pipe("../tennis.mp4", model);

	//processs the video and track the players
	if((trackPlayer != NULL) && (strcmp(trackPlayer, "t")==0 || strcmp(trackPlayer, "true")==0)){	
		Pipeline pipe (videoFile, outFile, model, true);	
		pipe.process();
	}
	//processs the video without tracking the players
	else{
		Pipeline pipe(videoFile, outFile, model);	
		pipe.process();
	}
	

	return 0;
}